package com.itheima.dao.Repository;

import com.itheima.model.domain.BusUser;
import com.itheima.model.domain.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import javax.transaction.Transactional;
import java.util.List;

public interface BusUserResponsitory extends  JpaRepository<BusUser,Integer>{



        List<BusUser> findAll();

        @Query(value = "select a from BusUser a where a.bus_user_name=:username and a.bus_user_password=:password")
        BusUser loginConfirm(@Param("username") String username, @Param("password") String password);

        @Query(value = "select a from BusUser  a where a.bus_user_name=:user_name")
        BusUser findUserByName(@Param("user_name")String user_name);

        @Query(value="SELECT a FROM BusUser a WHERE a.bus_user_name LIKE %:keyword%")
        List<BusUser> findByUsernameContaining(String keyword);

        @Transactional
        @Modifying
        @Query("DELETE FROM BusUser a WHERE a.bus_user_id = :id")
        void deleteBusUserByUserId(int id);

        @Query(value = "select a from BusUser  a where a.bus_user_id=:id")
        BusUser findUserById(@Param("id")int id);

        @Transactional
        @Modifying
        @Query("UPDATE BusUser a SET a.bus_user_name = :#{#user.bus_user_name}, a.bus_user_password = :#{#user.bus_user_password} , a.bus_user_tel = :#{#user.bus_user_tel} , a.bus_user_desc = :#{#user.bus_user_desc} , a.bus_user_address = :#{#user.bus_user_address}, a.bus_user_averagepriece= :#{#user.bus_user_averagepriece} , a.bus_user_image= :#{#user.bus_user_image} WHERE a.bus_user_id = :#{#user.bus_user_id}")
        void updateBusUserInfo(@Param("user") BusUser user);
}
